<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

/*************************************************************************
	Processing of received parameters
*************************************************************************/
if(!isset($arParams["CACHE_TIME"]))
	$arParams["CACHE_TIME"] = 3600;

$arParams["IBLOCK_TYPE"] = trim($arParams["IBLOCK_TYPE"]);
$arParams["IBLOCK_ID"] = intval($arParams["IBLOCK_ID"]);

$arParams["SECTION_ID"] = intval($arParams["SECTION_ID"]);

$arResult["SECTIONS"]=array();

/*************************************************************************
			Work with cache
*************************************************************************/
if($this->StartResultCache(false, Array($USER->GetGroups(), $arParams["SECTION_ID"])))
{
	if(!CModule::IncludeModule("iblock"))
	{
		$this->AbortResultCache();
		ShowError("Модуль Информационных блоков не найден");
		return;
	}
	
	if ( $arParams["SECTION_ID"] > 0 )
	{
		if ( $arParams["SET_SECTIONS_CHAIN"]=="Y" )
		{
			$sect_res = array("URL"=>$APPLICATION->GetCurDir(), "NAME"=>"Доска объявлений");
			$arResult["SECTIONS"][] = $sect_res;
		}

		$navs = CIBlockSection::GetNavChain($arParams["IBLOCK_ID"], $arParams["SECTION_ID"]);
		while($nav = $navs->GetNext())
		{
			$sect_res["URL"] = $nav["SECTION_PAGE_URL"];
			$sect_res["NAME"] = $nav["NAME"];
			$arResult["SECTIONS"][] = $sect_res;
			$arResult["TITLE"] = $nav["NAME"];		
		}
	}
	else
	{
		$rsSections = CIBlockSection::GetList(
				Array("left_margin"=>"asc"), 
				Array("ACTIVE" => "Y","IBLOCK_ID" => $arParams["IBLOCK_ID"],"SECTION_ID" => "0", "CNT_ACTIVE"=>"Y"),
				true
		);
		while ( $sect = $rsSections->GetNext() )
		{
			$rsSections2 = CIBlockSection::GetList(
					Array("left_margin"=>"asc"), 
					Array("ACTIVE" => "Y","IBLOCK_ID" => $arParams["IBLOCK_ID"],"SECTION_ID" => $sect["ID"], "CNT_ACTIVE"=>"Y"),
					true
			);
			while ( $sect2 = $rsSections2->GetNext() )
				$sect["SECTIONS"][] = $sect2;
				
			$arResult["SECTIONS"][] = $sect;
		}
		$arResult["TITLE"] = $APPLICATION->GetTitle();
	}

	$this->IncludeComponentTemplate();
}

if ( count($arResult["SECTIONS"])>0 )
{
	if ($arParams["SET_TITLE"]=="Y")
	{
		$APPLICATION->SetTitle($arResult["TITLE"]);
	}

	if($arParams["ADD_SECTIONS_CHAIN"] && ($arParams["SECTION_ID"]>0))
	{
		foreach($arResult["SECTIONS"] as $arPath)
		{
			$APPLICATION->AddChainItem($arPath["NAME"], $arPath["URL"]);
		}
	}
}
?>